using System;
using System.Collections.Generic;
using System.Collections.Specialized;

namespace HooIzDat.Web.Models
{
    public partial class Trait
    {
        public virtual int TraitID
        {
            get;
            set;
        }
    
        public virtual Nullable<int> ParentTraitID
        {
            get { return _parentTraitID; }
            set
            {
                try
                {
                    _settingFK = true;
                    if (_parentTraitID != value)
                    {
                        if (Trait2 != null && Trait2.TraitID != value)
                        {
                            Trait2 = null;
                        }
                        _parentTraitID = value;
                    }
                }
                finally
                {
                    _settingFK = false;
                }
            }
        }
        private Nullable<int> _parentTraitID;
    
        public virtual string Name
        {
            get;
            set;
        }
    
        public virtual string QuestionFormat
        {
            get;
            set;
        }

        public virtual ICollection<CharacterTrait> CharacterTraits
        {
            get
            {
                if (_characterTraits == null)
                {
                    var newCollection = new FixupCollection<CharacterTrait>();
                    newCollection.CollectionChanged += FixupCharacterTraits;
                    _characterTraits = newCollection;
                }
                return _characterTraits;
            }
            set
            {
                if (!ReferenceEquals(_characterTraits, value))
                {
                    var previousValue = _characterTraits as FixupCollection<CharacterTrait>;
                    if (previousValue != null)
                    {
                        previousValue.CollectionChanged -= FixupCharacterTraits;
                    }
                    _characterTraits = value;
                    var newValue = value as FixupCollection<CharacterTrait>;
                    if (newValue != null)
                    {
                        newValue.CollectionChanged += FixupCharacterTraits;
                    }
                }
            }
        }
        private ICollection<CharacterTrait> _characterTraits;
    
        public virtual ICollection<Trait> Trait1
        {
            get
            {
                if (_trait1 == null)
                {
                    var newCollection = new FixupCollection<Trait>();
                    newCollection.CollectionChanged += FixupTrait1;
                    _trait1 = newCollection;
                }
                return _trait1;
            }
            set
            {
                if (!ReferenceEquals(_trait1, value))
                {
                    var previousValue = _trait1 as FixupCollection<Trait>;
                    if (previousValue != null)
                    {
                        previousValue.CollectionChanged -= FixupTrait1;
                    }
                    _trait1 = value;
                    var newValue = value as FixupCollection<Trait>;
                    if (newValue != null)
                    {
                        newValue.CollectionChanged += FixupTrait1;
                    }
                }
            }
        }
        private ICollection<Trait> _trait1;
    
        public virtual Trait Trait2
        {
            get { return _trait2; }
            set
            {
                if (!ReferenceEquals(_trait2, value))
                {
                    var previousValue = _trait2;
                    _trait2 = value;
                    FixupTrait2(previousValue);
                }
            }
        }
        private Trait _trait2;
    
        public virtual ICollection<TraitVariation> TraitVariations
        {
            get
            {
                if (_traitVariations == null)
                {
                    var newCollection = new FixupCollection<TraitVariation>();
                    newCollection.CollectionChanged += FixupTraitVariations;
                    _traitVariations = newCollection;
                }
                return _traitVariations;
            }
            set
            {
                if (!ReferenceEquals(_traitVariations, value))
                {
                    var previousValue = _traitVariations as FixupCollection<TraitVariation>;
                    if (previousValue != null)
                    {
                        previousValue.CollectionChanged -= FixupTraitVariations;
                    }
                    _traitVariations = value;
                    var newValue = value as FixupCollection<TraitVariation>;
                    if (newValue != null)
                    {
                        newValue.CollectionChanged += FixupTraitVariations;
                    }
                }
            }
        }
        private ICollection<TraitVariation> _traitVariations;

        private bool _settingFK = false;
    
        private void FixupTrait2(Trait previousValue)
        {
            if (previousValue != null && previousValue.Trait1.Contains(this))
            {
                previousValue.Trait1.Remove(this);
            }
    
            if (Trait2 != null)
            {
                if (!Trait2.Trait1.Contains(this))
                {
                    Trait2.Trait1.Add(this);
                }
                if (ParentTraitID != Trait2.TraitID)
                {
                    ParentTraitID = Trait2.TraitID;
                }
            }
            else if (!_settingFK)
            {
                ParentTraitID = null;
            }
        }
    
        private void FixupCharacterTraits(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.NewItems != null)
            {
                foreach (CharacterTrait item in e.NewItems)
                {
                    item.Trait = this;
                }
            }
    
            if (e.OldItems != null)
            {
                foreach (CharacterTrait item in e.OldItems)
                {
                    if (ReferenceEquals(item.Trait, this))
                    {
                        item.Trait = null;
                    }
                }
            }
        }
    
        private void FixupTrait1(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.NewItems != null)
            {
                foreach (Trait item in e.NewItems)
                {
                    item.Trait2 = this;
                }
            }
    
            if (e.OldItems != null)
            {
                foreach (Trait item in e.OldItems)
                {
                    if (ReferenceEquals(item.Trait2, this))
                    {
                        item.Trait2 = null;
                    }
                }
            }
        }
    
        private void FixupTraitVariations(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.NewItems != null)
            {
                foreach (TraitVariation item in e.NewItems)
                {
                    item.Trait = this;
                }
            }
    
            if (e.OldItems != null)
            {
                foreach (TraitVariation item in e.OldItems)
                {
                    if (ReferenceEquals(item.Trait, this))
                    {
                        item.Trait = null;
                    }
                }
            }
        }
    }
}
